package com.gensdai.leliang.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gensdai.leliang.R;
import com.gensdai.leliang.activity.New_delivery_address;
import com.gensdai.leliang.entity.Address;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/6/2.
 */

public class Delivery_address_adapter extends BaseAdapter implements View.OnClickListener {

    Context mContext;
    List<Address> list;
    Honder honder;
    setdata mSd = null;

    public Delivery_address_adapter(Context context, List<Address> list, setdata sd) {
        mContext = context;
        this.list = list;
        mSd = sd;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            honder = new Honder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.delivery_address_list, null);
            honder.name = (TextView) convertView.findViewById(R.id.name);
            honder.phone = (TextView) convertView.findViewById(R.id.phone);
            honder.address = (TextView) convertView.findViewById(R.id.address);
            honder.address_default = (TextView) convertView.findViewById(R.id.address_default);
            honder.editor = (TextView) convertView.findViewById(R.id.editor);
            honder.delete = (TextView) convertView.findViewById(R.id.delete);
            convertView.setTag(honder);
        } else {
            honder = (Honder) convertView.getTag();
        }
        honder.editor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent go_new_address = new Intent(mContext, New_delivery_address.class);
                go_new_address.putExtra("address", (Serializable) list.get(position));
                mContext.startActivity(go_new_address);
            }
        });
        honder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSd != null) {
                    mSd.deleteListData(position);
                }
            }
        });
        honder.address_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSd != null && !list.get(position).getDefault().equals("1")) {
                    mSd.defaultListData(position);
                }
            }
        });
        if (list.get(position).getDefault().equals("1")) {
            Drawable img = ContextCompat.getDrawable(mContext, R.mipmap.mrdz);
            img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
            honder.address_default.setCompoundDrawables(img, null, null, null);
            honder.address_default.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark));
        } else {
            honder.address_default.setTextColor(ContextCompat.getColor(mContext, R.color.liuge6));
            Drawable img = ContextCompat.getDrawable(mContext, R.mipmap.yuan);
            img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
            honder.address_default.setCompoundDrawables(img, null, null, null);
        }

        honder.name.setText(list.get(position).getUser_name());
        honder.phone.setText(list.get(position).getPhone());
        honder.address.setText(list.get(position).getProvinceId() + list.get(position).getCityId() + list.get(position).getDistrictId()+ " " +list.get(position).getAddress());

        return convertView;
    }

    public interface setdata {
        void deleteListData(int position);

        void defaultListData(int position);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.editor:
                break;
            case R.id.delete:
                break;
            default:
                break;
        }
    }

    class Honder {
        TextView name;
        TextView phone;
        TextView address;
        TextView address_default;
        TextView editor;
        TextView delete;
    }

}
